﻿using MediatR;
using Production_Get.API.Application.Commands.Supplier;
using Production_Get.API.DTO;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using THE_WIND_RISES.Common.Domans.Produce.Procurement;
using THE_WIND_RISES.Common.Domans.Produce.Supplier;
using THE_WIND_RISES.Common.Dtos;
namespace Production_Get.API.Application.Commands.Procurement
{
    public class PurchasingsQueryHandler : IRequestHandler<PurchasingsQueryCommand, Result>
    {
        /// <summary>
        /// 采购/委外订单仓储
        /// </summary>
        private readonly IBaseRepositoryGet<PurchasingDto> purchasingsRepository;
        private readonly ILogger<PurchasingsQueryHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="purchasingsRepository"></param>
        /// <param name="logger"></param>
        public PurchasingsQueryHandler(IBaseRepositoryGet<PurchasingDto> purchasingsRepository, ILogger<PurchasingsQueryHandler> logger)
        {
            this.purchasingsRepository = purchasingsRepository;
            this.logger = logger;
        }
        public async Task<Result> Handle(PurchasingsQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Search };

            try
            {
                string sql = "select a.PurchasinId,a.BillMaking,a.HandleTime,a.PurchasingNo,a.PurchasingName,a.SignUpTime,a.PurchasingStatus,a.PurchasingTypeId,b.        PurchasingTypeName,a.ProjectId,c.ProjectName,a.SuppliersId,d.SuppliersName,a.DepartmentId,e.DepartmentName,a.ApprovalStatus from Purchasings a inner join PurchasingType b on a.PurchasingTypeId=b.PurchasingTypeId  join Project c on a.ProjectId=c.ProjectId join Suppliers d on a.SuppliersId=d.SuppliersId join Department e on a.DepartmentId=e.DepartmentId  where a.IsDelete=0";

                var list = purchasingsRepository.GetAll(sql, default).ToList();

                if (!string.IsNullOrEmpty(request.PurchasingNo))
                {
                    list = list.Where(x => x.PurchasingNo.Contains(request.PurchasingNo)).ToList();
                }

                if (!string.IsNullOrEmpty(request.PurchasingName))
                {
                    list = list.Where(x => x.PurchasingName.Contains(request.PurchasingName)).ToList();
                }
                if (request.PurchasingTypeId > 0)
                {
                    list = list.Where(x => x.PurchasingTypeId == request.PurchasingTypeId).ToList();
                }

                if (request.ProjectId > 0)
                {
                    list = list.Where(x => x.ProjectId == request.ProjectId).ToList();
                }
                if (request.SuppliersId > 0)
                {
                    list = list.Where(x => x.SuppliersId == request.SuppliersId).ToList();
                }
                if (request.PurchasingStatus >0)
                {
                    list = list.Where(x => x.PurchasingStatus == request.PurchasingStatus).ToList();
                }
                var totalCount = list.Count();

                var totalPage = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);

                list = list.OrderBy(x => x.PurchasinId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

                var aa = new
                {
                    list,
                    totalCount,
                    totalPage
                };
                if (list != null)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = aa;
                    result.Msg = "显示成功";
                    logger.LogInformation("采购/委外订单显示成功");
                }
                else
                {
                    result.Msg = "显示失败";
                }
            }
            catch (Exception ex)
            {
                logger.LogError("采购/委外订单显示失败" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
